package  
{
	import org.flixel.FlxGroup;
	import org.flixel.FlxSprite;
	import org.flixel.FlxG;
	
	/**
	 * ...
	 * @author João Brant
	 * @author Lucas Mattos
	 */
	public class Rua extends FlxGroup
	{
		public var x:int;
		public var y:int;
		
		public var streetStyle:int = 0;
		public var streetStyleTime:int = 0;
		
		public var meioFioStyle:int = 3;
		public var meioFioStyleTime:int = 0;
		
		[Embed(source = "../assets/envy/r1.png")]
		public static var ruaMeioFioImg:Class;
		
		[Embed(source = "../assets/envy/r2.png")]
		public static var ruaMeioFio2Img:Class;
		
		[Embed(source = "../assets/envy/r3.png")]
		public static var rua1Img:Class;
		
		[Embed(source = "../assets/envy/r4.png")]
		public static var rua2Img:Class;
		
		[Embed(source = "../assets/envy/r5.png")]
		public static var rua3Img:Class;
		
		[Embed(source = "../assets/envy/m1s1.png")]
		public static var m1s1Img:Class;
		[Embed(source = "../assets/envy/m2s1.png")]
		public static var m2s1Img:Class;
		[Embed(source = "../assets/envy/m3s1.png")]
		public static var m3s1Img:Class;
		[Embed(source = "../assets/envy/m4s1.png")]
		public static var m4s1Img:Class;
		[Embed(source = "../assets/envy/m5s1.png")]
		public static var m5s1Img:Class;
		[Embed(source = "../assets/envy/m6s1.png")]
		public static var m6s1Img:Class;
		[Embed(source = "../assets/envy/m7s1.png")]
		public static var m7s1Img:Class;
		
		[Embed(source = "../assets/envy/m1s2.png")]
		public static var m1s2Img:Class;
		[Embed(source = "../assets/envy/m2s2.png")]
		public static var m2s2Img:Class;
		[Embed(source = "../assets/envy/m3s2.png")]
		public static var m3s2Img:Class;
		[Embed(source = "../assets/envy/m4s2.png")]
		public static var m4s2Img:Class;
		
		[Embed(source = "../assets/envy/m1s3.png")]
		public static var m1s3Img:Class;
		
		public function Rua() 
		{
			super();
			
		}
		public override function update():void
		{			
			var newParte:ParteRua;
			var anotherParte:ParteRua;
			
			/*for each (var part:ParteRua in members)
			{
				part.move(MainState.levelVelocity);
			}*/
			
			if (this.members.length == 0)
			{
				//RuaMeioFio
				newParte = createRuaMeioFio(x, y);
				
				//Rua
				anotherParte = createRua(x, newParte.y + newParte.height);
				
				//MeioFio
				createMeioFio(9, x, y);
			}
			
			this.sort("x", FlxGroup.DESCENDING);
			
						
			while (members[0].x + members[0].width < Main.windowWidth+FlxG.camera.scroll.x)
			{
				//trace(" " + (x + members[0].x + members[0].width));
				//RuaMeioFio
				newParte = createRuaMeioFio(x + members[0].x + members[0].width, y);
				
				//Rua
				anotherParte = createRua(newParte.x, newParte.y+newParte.height);
				
				//MeioFio
				createMeioFio(9, x + members[0].x + members[0].width, y);
				
				this.sort("x", FlxGroup.DESCENDING);
			}			
		}
		
		private function createMeioFio(n:int, posx:int, posy:int):ParteRua
		{
			var newParte:ParteRua;
			var oldParte:ParteRua = null;
			var i:int = 0;
			var rand:int;
			
			meioFioStyleTime++;
			
			if (meioFioStyleTime >= 10)
			{
				rand = (Math.random() * 4);
				
				switch(rand)
				{
					case 0:
						if(meioFioStyle == 0) break;
						meioFioStyle = 0;
						meioFioStyleTime = 0;
						break;
					case 1:
						if (meioFioStyle == 1) break;
						meioFioStyle = 1;
						meioFioStyleTime = 0;
						break;
					case 2:
						if (meioFioStyle == 2) break;
						meioFioStyle = 2;
						meioFioStyleTime = 0;
						break;
				}
			}	
			
			for (i = 0; i < n; i++)
			{				
				if (meioFioStyle == 0)
				{					
					rand = (Math.random() * 100);
						
					switch(rand)
					{
						case 0:	newParte = new ParteRua(m1s1Img); break;
						case 1:	newParte = new ParteRua(m2s1Img); break;
						case 2:	newParte = new ParteRua(m3s1Img); break;
						case 3:	newParte = new ParteRua(m4s1Img); break;
						case 4:	newParte = new ParteRua(m5s1Img); break;
						case 5:	newParte = new ParteRua(m6s1Img); break;
						case 6:	newParte = new ParteRua(m7s1Img); break;
						default: newParte = new ParteRua(m1s1Img); break;
					}
				}				
				else if (meioFioStyle == 1)
				{
					rand = (Math.random() * 4);
					
					switch(rand)
					{
						case 0:	newParte = new ParteRua(m1s2Img); break;
						case 1:	newParte = new ParteRua(m2s2Img); break;
						case 2:	newParte = new ParteRua(m3s2Img); break;
						case 3:	newParte = new ParteRua(m4s2Img); break;
						default: newParte = new ParteRua(m1s2Img); break;
					}	
				}				
				else
				{
					if (i == 0 || i == 1)
					{
						rand = (Math.random() * 100);
						
						switch(rand)
						{
							case 0:	newParte = new ParteRua(m1s1Img); break;
							case 1:	newParte = new ParteRua(m2s1Img); break;
							case 2:	newParte = new ParteRua(m3s1Img); break;
							case 3:	newParte = new ParteRua(m4s1Img); break;
							case 4:	newParte = new ParteRua(m5s1Img); break;
							case 5:	newParte = new ParteRua(m6s1Img); break;
							case 6:	newParte = new ParteRua(m7s1Img); break;
							default: newParte = new ParteRua(m1s1Img); break;
						}	
					}
					else
					{
						newParte = new ParteRua(m1s3Img);
					}
				}
				
				if (oldParte == null)
				{
					newParte.x = posx;
					newParte.y = posy - newParte.height;
					
					add(newParte);
					oldParte = newParte;
				}
				else
				{
					newParte.x = oldParte.x;
					newParte.y = oldParte.y - newParte.height;
					
					add(newParte);
					oldParte = newParte;
				}
			}
									
			return newParte;
		}
		
		private function createRuaMeioFio(posx:int, posy:int):ParteRua
		{
			var newParte:ParteRua;
			
			var rand:int = Math.random()*10;
			
			switch(rand)
			{
				case 0:
					newParte = new ParteRua(ruaMeioFio2Img); break;
				default:
					newParte = new ParteRua(ruaMeioFioImg); break;
			}
					
			newParte.x = posx;
			newParte.y = posy;
			add(newParte);
			
			return newParte;
		}
		
		private function createRua(posx:int, posy:int):ParteRua
		{
			var newParte:ParteRua;
			
			streetStyleTime++;
			
			if (streetStyleTime >= 4 && streetStyle == 0)
			{
				streetStyle = 1;
				streetStyleTime = 0;
			}
			else if (streetStyleTime >= 10)
			{
				var rand:int = (Math.random() * 4);
				
				switch(rand)
				{
					case 0:
						if(streetStyle == 0) break;
						streetStyle = 0;
						streetStyleTime = 0;
						break;
					case 1:
						if (streetStyle == 1) break;
						streetStyle = 1;
						streetStyleTime = 0;
						break;
				}
			}
			
			if (streetStyleTime == 0 || streetStyleTime==1)
				newParte = new ParteRua(rua3Img);
			else
			{				
				if (streetStyle == 1)
					newParte = new ParteRua(rua2Img);
				else
					newParte = new ParteRua(rua1Img);
			}
					
			newParte.x = posx;
			newParte.y = posy;
			
			add(newParte);
			return newParte;
		}
	}
	

}